#!/bin/sh
main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html

cat <<'EOF'
<script>
function del_netzone(zonetype, netzone)
{
if (confirm("确定要删除 " + zonetype + " " + netzone + " ?")) {
    var data = "app=netzone&action=post_"+ zonetype + "_delete&netzone=" + netzone;
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'ajax-fluid');
	setTimeout("window.location.reload();", 5000);
  }
}
</script>
EOF
lan_list_all=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_available_lanzone`
lan_list=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_available_lanzone no="ext_lan"`
ext_lan_list=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_available_lanzone no="lan"`
wan_list=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_available_wanzone`
dev_list=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_dev_all no="wan" | grep -v "^wlan"`
dev_free_list=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_dev_free`
firewall_str=`uci show firewall`
cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
				<h3>设备所属网络区</h3>
					<table class="pure-table pure-table-bordered">
					  <thead>
						<tr>
							<th>
								网卡设备
							</th>
							<th>
								内网网络区
							</th>
							<th>
								操作
							</th>
						</tr>
					  </thead>
					  <tbody>
EOF
for dev in $dev_list
do
dev_num=`echo ${dev} | md5sum | cut -d ' ' -f1`
cat <<EOF
<script>
\$(function(){
  \$('#dev_belong_${dev_num}').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_dev_save&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'dev_belong_${dev_num}');
  });
});
</script>
EOF
lan_now=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin query_dev dev="${dev}"`

cat <<EOF
<form id="dev_belong_${dev_num}">
						<tr>
							<td>
								${dev}
							</td>
							<td>
								<select name="lan_zone">
EOF
	for lan_zone in $lan_list
	do
	cat <<EOF
									<option value="${lan_zone}" `[ "${lan_zone}" = "$lan_now" ] && echo selected`>${lan_zone}</option>
EOF
	done
cat <<EOF
									<option `[ -z "$lan_now" ] && echo selected`>未定义</option>
								</select>
							</td>
							<td>
								<input type="hidden" name="dev" value="${dev}">
								<button type="submit" class="pure-button">保存</button>
							</td>
						</tr>
</form>
EOF
done
cat <<EOF
					  </tbody>
					</table>

			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
				<h3>局域网网络区所属广域网网络区</h3>
					
					<table class="pure-table pure-table-bordered">
					  <thead>
						<tr>
							<th>
								内网网络区
							</th>
							<th>
								外网网络区
							</th>
							<th>
								操作
							</th>
						</tr>
					  </thead>
					  <tbody>
EOF
for lan_zone in $lan_list
do
lan_zone_num=`echo ${lan_zone} | md5sum | cut -d ' ' -f1`
lan_belong_str=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin query_lan dev_zone=${lan_zone}`
cat <<EOF
<script>
\$(function(){
  \$('#lan_zone_nat_from_${lan_zone_num}').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_lan_save&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'lan_zone_nat_from_${lan_zone_num}');
  });
});
</script>
EOF

cat <<EOF
<form id="lan_zone_nat_from_${lan_zone_num}">
						<tr>
							<td>
								${lan_zone}
							</td>
							<td>
EOF
	for wan_zone in $wan_list
	do
	cat <<EOF
								<label for="${lan_zone}_nat_from_${wan_zone}" class="pure-checkbox">
									<input name="wan_zone_${wan_zone}" value="${wan_zone}" id="${lan_zone}_nat_from_${wan_zone}" type="checkbox" `echo "$lan_belong_str" | grep -q "^${wan_zone}$" && echo checked`> ${wan_zone}
								</label>
EOF
	done

cat <<EOF
							</td>
							<td>
								<input type="hidden" name="lan_zone" value="${lan_zone}">
								<button type="submit" class="pure-button">保存</button>
							</td>
						</tr>
</form>
EOF
done
cat <<EOF
					  </tbody>
					</table>
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-1">
				<h3>广域网类型网络区管理</h3>
					<table class="pure-table pure-table-bordered">
					  <thead>
						<tr>
							<th>
								广域网网络区
							</th>
							<th>
								使用网卡
							</th>
							<th>
								防火墙策略
							</th>
							<th>
								其它
							</th>
							<th>
								操作
							</th>
						</tr>
					  </thead>
					  <tbody>
EOF

for wan_zone in $wan_list
do
wan_zone_num=`echo ${wan_zone} | md5sum | cut -d ' ' -f1`
cat <<EOF
<script>
\$(function(){
  \$('#wan_new_${wan_zone_num}').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_wan_new&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'wan_new_${wan_zone_num}');
	setTimeout("window.location.reload();", 5000);
  });
});
</script>
EOF

cat <<EOF
<form id="wan_new_${wan_zone_num}" class="pure-form">
						<tr>
							<td>
								${wan_zone}
							</td>
							<td>
								<datalist id="${wan_zone}">
								选择一个
								<select name="${wan_zone}_sel">
EOF
	for dev in $dev_free_list
	do
	cat <<EOF
									<option value="${dev}">${dev}</option>
EOF
done

cat <<EOF
								</select>  
								输入
								</datalist>
								<input type="text" name="dev" value="`uci get network.${wan_zone}.ifname`" list="${wan_zone}" />

							</td>
EOF
wan_zone_config_name=$(echo "$firewall_str" | grep firewall.@zone | grep "\.name=${wan_zone}$" | sed 's/\.name=.*//')

cat <<EOF
							<td>
								<label for="${wan_zone_config_name}_input" class="pure-checkbox">
									<input id="${wan_zone_config_name}_input" type="checkbox" value="1" name="wan_input" `[ "$(uci get $wan_zone_config_name'.input')" = "ACCEPT" ] && echo checked`> input
								</label>
								<label for="${wan_zone_config_name}_output" class="pure-checkbox">
									<input id="${wan_zone_config_name}_output" type="checkbox" value="1" name="wan_output" `[ "$(uci get $wan_zone_config_name'.output')" = "ACCEPT" ] && echo checked`> output
								</label>
								<label for="${wan_zone_config_name}_forward" class="pure-checkbox">
									<input id="${wan_zone_config_name}_forward" type="checkbox" value="1" name="wan_forward" `[ "$(uci get $wan_zone_config_name'.forward')" = "ACCEPT" ] && echo checked`> forward
								</label>
							</td>
							<td>
								<label for="${wan_zone_config_name}_force_link" class="pure-checkbox">
									<input id="${wan_zone_config_name}_force_link" type="checkbox" value="1" name="wan_force_link" `[ "$(uci get network.${wan_zone}.force_link 2>/dev/null)" = "1" ] && echo checked`> force link
								</label>
							</td>
							<td>
								<input type="hidden" name="wan_zone" value="${wan_zone}">
								<button type="submit" class="pure-button">保存</button>
								<a onclick="del_netzone('wan','${wan_zone}')" class="pure-button button-error">删除</a>
							</td>
						</tr>
</form>
EOF
done

cat <<EOF
<script>
\$(function(){
  \$('#wan_new_new').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_wan_new&new=1&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'wan_new_new');
	setTimeout("window.location.reload();", 5000);
  });
});
</script>
<form id="wan_new_new" class="pure-form">
						<tr>
							<th>
								<label for="wanzone_prefix">wan_</label><input type="text" id="wanzone_prefix" name="wan_zone" style="width:100px"/>
							</th>
							<td>
								<datalist id="wan_zone_new">
EOF
	for dev in $dev_free_list
	do
	cat <<EOF
									<option value="${dev}">${dev}</option>
EOF
done

cat <<EOF
								</select>  
								输入
								</datalist>
								<input type="text" name="dev" value="" list="wan_zone_new" />
							</td>
							<td>
								<label for="wan_zone_new_input" class="pure-checkbox">
									<input id="wan_zone_new_input" value="1" type="checkbox" name="wan_input"> input
								</label>
								<label for="wan_zone_new_output" class="pure-checkbox">
									<input id="wan_zone_new_output" value="1" type="checkbox" name="wan_output" checked> output
								</label>
								<label for="wan_zone_new_forward" class="pure-checkbox">
									<input id="wan_zone_new_forward" value="1" type="checkbox" name="wan_forward"> forward
								</label>
							</td>
							<td>
								<label for="wan_zone_new_force_link" class="pure-checkbox">
									<input id="wan_zone_new_force_link" value="1" type="checkbox" name="wan_force_link" checked> force link
								</label>
							</td>
							<td>
								<button type="submit" class="pure-button">添加</button>
							</td>
						</tr>
</form>
					  </tbody>
					</table>
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-1">
				<h3>局域网类型网络区管理</h3>
					<table class="pure-table pure-table-bordered">
					  <thead>
						<tr>
							<th>
								局域网网络区
							</th>
							<th>
								IP地址
							</th>
							<th>
								掩码
							</th>
							<th>
								防火墙策略
							</th>
							<th>
								其它
							</th>
							<th>
								操作
							</th>
						</tr>
					  </thead>
					  <tbody>
EOF
for lan_zone in $lan_list
do
lan_zone_num=`echo ${lan_zone} | md5sum | cut -d ' ' -f1`
cat <<EOF
<script>
\$(function(){
  \$('#lan_new_${lan_zone_num}').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_lan_new&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'lan_new_${lan_zone_num}');
	setTimeout("window.location.reload();", 5000);
  });
});
</script>
EOF
cat <<EOF
<form id="lan_new_${lan_zone_num}" class="pure-form">

						<tr>
							<td>
								${lan_zone}
							</td>
							<td>
								<input type="text" name="lan_ip" value="`uci get network.${lan_zone}.ipaddr`"></input>
							</td>
							<td>
								<input type="text" name="lan_netmask" value="`uci get network.${lan_zone}.netmask`"></input>
							</td>
EOF
lan_zone_config_name=$(echo "$firewall_str" | grep firewall.@zone | grep "\.name=${lan_zone}$" | sed 's/\.name=.*//')

cat <<EOF
							<td>
								<label for="${lan_zone_config_name}_input" class="pure-checkbox">
									<input id="${lan_zone_config_name}_input" value="1" name="lan_input" type="checkbox" `[ "$(uci get $lan_zone_config_name'.input')" = "ACCEPT" ] && echo checked`> input
								</label>
								<label for="${lan_zone_config_name}_output" class="pure-checkbox">
									<input id="${lan_zone_config_name}_output" value="1" name="lan_output" type="checkbox" `[ "$(uci get $lan_zone_config_name'.output')" = "ACCEPT" ] && echo checked`> output
								</label>
								<label for="${lan_zone_config_name}_forward" class="pure-checkbox">
									<input id="${lan_zone_config_name}_forward" value="1" name="lan_forward" type="checkbox" `[ "$(uci get $lan_zone_config_name'.forward')" = "ACCEPT" ] && echo checked`> forward
								</label>
							</td>
							<td>
								<label for="${lan_zone_config_name}_force_link" class="pure-checkbox">
									<input id="${lan_zone_config_name}_force_link" value="1" name="lan_zone" name="lan_force_link" type="checkbox" `[ "$(uci get network.${lan_zone}.force_link 2>/dev/null)" = "1" ] && echo checked`> force link
								</label>
							</td>
							<td>
								<input type="hidden" name="lan_zone" value="${lan_zone}">
								<button class="pure-button">保存</button>
								<a onclick="del_netzone('lan','${lan_zone}')" class="pure-button button-error">删除</a>
							</td>
						</tr>
</form>
				
EOF
done

cat <<EOF

<script>
\$(function(){
  \$('#lan_add_new').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_lan_new&new=1&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'lan_add_new');
	setTimeout("window.location.reload();", 5000);
  });
});
</script>
<form id="lan_add_new" class="pure-form">
						<tr>
							<td>
								<label for="lanzone_prefix">lan_</label><input type="text" id="lanzone_prefix" name="lan_zone" style="width:100px"/>
							</td>
							<td>
								<input type="text" name="lan_ip" value=""></input>
							</td>
							<td>
								<input type="text" name="lan_netmask" value=""></input>
							</td>
							<td>
								<label for="lan_add_new_input" class="pure-checkbox">
									<input id="lan_add_new_input" value="1" name="lan_input" type="checkbox" checked> input
								</label>
								<label for="lan_add_new_output" class="pure-checkbox">
									<input id="lan_add_new_output" value="1" name="lan_output" type="checkbox" checked> output
								</label>
								<label for="lan_add_new_forward" class="pure-checkbox">
									<input id="lan_add_new_forward" value="1" name="lan_forward" type="checkbox" checked> forward
								</label>
							</td>
							<td>
								<label for="lan_add_new_force_link" class="pure-checkbox">
									<input id="lan_add_new_force_link" value="1" name="lan_force_link" type="checkbox" checked> force link
								</label>
							</td>
							<td>
								<button type="submit" class="pure-button">添加</button>
							</td>
						</tr>
</form>
					  </tbody>
					</table>
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-1">
				<h3>局域网类型网络区拓展网段管理</h3>
					<table class="pure-table pure-table-bordered">
					  <thead>
						<tr>
							<th>
								拓展网段名称
							</th>
							<th>
								所属内网网络区
							</th>
							<th>
								IP地址
							</th>
							<th>
								掩码
							</th>
							<th>
								防火墙策略
							</th>
							<th>
								操作
							</th>
						</tr>
					  </thead>
					  <tbody>
EOF
for ext_lan_zone in $ext_lan_list
do
lan_zone_num=`echo ${ext_lan_zone} | md5sum | cut -d ' ' -f1`
ext_tar_now=`$DOCUMENT_ROOT/apps/netzone/netzone.sbin query_ext_lan dev_zone="${ext_lan_zone}"`
cat <<EOF
<script>
\$(function(){
  \$('#lan_new_${lan_zone_num}').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_lan_new&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'lan_new_${lan_zone_num}');
	setTimeout("window.location.reload();", 5000);
  });
});
</script>
EOF
cat <<EOF
<form id="lan_new_${lan_zone_num}" class="pure-form pure-g">
						<tr>
							<td>
								${ext_lan_zone}
								<input type="hidden" name="lan_zone" value="${ext_lan_zone}"></input>
							</td>
							<td>
								<select name="ext_tar">
EOF
	for lan_zone in $lan_list
	do
	cat <<EOF
									<option `[ "$ext_tar_now" = "${lan_zone}" ] && echo selected`>${lan_zone}</option>
EOF
	done
cat <<EOF
								</select>
							</td>
							<td>
								<input type="text" name="lan_ip" value="`uci get network.${ext_lan_zone}.ipaddr`"></input>
							</td>
							<td>
								<input type="text" name="lan_netmask" value="`uci get network.${ext_lan_zone}.netmask`"></input>
							</td>
EOF
ext_lan_zone_config_name=$(echo "$firewall_str" | grep firewall.@zone | grep "\.name=${ext_lan_zone}$" | sed 's/\.name=.*//')

cat <<EOF
							<td>
								<label for="${ext_lan_zone_config_name}_input" class="pure-checkbox">
									<input id="${ext_lan_zone_config_name}_input" name="lan_input" value="1" type="checkbox" `[ "$(uci get $ext_lan_zone_config_name'.input')" = "ACCEPT" ] && echo checked`> input
								</label>
								<label for="${ext_lan_zone_config_name}_output" class="pure-checkbox">
									<input id="${ext_lan_zone_config_name}_output" name="lan_output" value="1" type="checkbox" `[ "$(uci get $ext_lan_zone_config_name'.output')" = "ACCEPT" ] && echo checked`> output
								</label>
								<label for="${ext_lan_zone_config_name}_forward" class="pure-checkbox">
									<input id="${ext_lan_zone_config_name}_forward" name="lan_forward" value="1" type="checkbox" `[ "$(uci get $ext_lan_zone_config_name'.forward')" = "ACCEPT" ] && echo checked`> forward
								</label>
							</td>
							<td>
								<button class="pure-button">保存</button>
								<a onclick="del_netzone('lan','${ext_lan_zone}')" class="pure-button button-error">删除</a>
							</td>
						</tr>
</form>		
						
EOF
done
cat <<EOF
<script>
\$(function(){
  \$('#ext_lan_add_new').on('submit', function(e){
    e.preventDefault();
    var data = "app=netzone&action=post_lan_new&new=1&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'ext_lan_add_new');
	setTimeout("window.location.reload();", 5000);
  });
});
</script>
<form id="ext_lan_add_new" class="pure-form pure-g">
						<tr>
							<th>
								<label for="ext_lanzone_prefix">ext_lan_</label><input type="text" id="ext_lanzone_prefix" name="lan_zone" style="width:100px"/>
							</th>
							<td>
								<select name="ext_tar">
EOF
	for lan_zone in $lan_list
	do
	cat <<EOF
									<option>${lan_zone}</option>
EOF
	done
cat <<EOF
								</select>
							</td>
							<td>
								<input type="text" name="lan_ip" value=""></input>
							</td>
							<td>
								<input type="text" name="lan_netmask" value=""></input>
							</td>
							<td>
								<label for="ext_lan_add_new_input" class="pure-checkbox">
									<input id="ext_lan_add_new_input" name="lan_input" value="1" type="checkbox" checked> input
								</label>
								<label for="ext_lan_add_new_output" class="pure-checkbox">
									<input id="ext_lan_add_new_output" name="lan_output" value="1" type="checkbox" checked> output
								</label>
								<label for="ext_lan_add_new_forward" class="pure-checkbox">
									<input id="ext_lan_add_new_forward" name="lan_forward" value="1" type="checkbox" checked> forward
								</label>
							</td>
							<td>
								<button type="submit" class="pure-button">添加</button>
							</td>
						</tr>
</form>
						
					  </tbody>
					</table>
			</div>
		</div>
EOF


footer_html
}
. $DOCUMENT_ROOT/apps/$FORM_app/netzone_lib.sh
lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
